2023 更新:想要透過 python 爬蟲下載網站大量圖片可參考:https://steam.oxxostudio.tw/category/python/spider/ptt-more-images.html
以下是參考網路教學實作把底下 google 上 doodle 的圖片下載至本機端
https://www.google.com/doodles?hl=zh-TW#archive
操作畫面:(篩選後的 json 資料)
網址解析 Part01
https
s = secure
網站使用編碼協定的安全性(secure) SSL
www.google.com.tw
Domain Name(域名) 192.168.1.78 IPv4 or IPV6
search? 查詢
q=hchs&rlz=1C5CHFA_enTW893TW895&oq=hchs&aqs=chrome..69i57j69i65l2.2623j0j7&**sourceid=chrome&ie=UTF-8
附加資訊
網址解析 Part02
觀察:
右鍵 查看網頁原始碼
CTRL + F 搜尋圖片關鍵字
解析:
Chrome 的 Network 工具
XHR工具列 點擊 6?hl=zh_TW
觀察右邊的 Header
Request URL: https://www.google.com/doodles/json/2020/6?hl=zh_TW
有了『年份』和『月份』的資訊
再看一下『preview』,有了 json 格式,以及爆炸多資訊
json 資料
中括號 (List) 和大括號 (Dic- tionary)
中括號 (List) 『同樣類型』的資料 排隊[1,2,3,4]
大括號 (Dic- tionary) 『不同資料』組合成一個『複雜資料』
List 和 Dictionary 組合形式 >>『JSON 格式』
from urllib.request import urlopen
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
response = urlopen(“https://www.google.com/doodles/json/2020/6?hl=zh_TW")
print(response)<http.client.HTTPResponse object at 0x10c708c40>
撈出資料了
json.load(response)
json 裡頭有 “url”: “title”:
preview 可以看得更清楚
還有翻譯 translated…的相關資訊
取出我們要的圖片標題及網址:
for d in doodles:
url = “https:” + d[“url”]
title = d[“title”]
print(“圖片標題:”, title) print(“圖片網址:”, url)
下載圖片:
使用 urlretrieve 功能
然後創立資料夾
將圖片下載回來
url = “https:” + d[“url”]
title = d[“title”]
fpath = “doodles/” + url.split(“/”)[-1]
urlretrieve(url, fpath)
最後成果:
參考資料:
https://stackoverflow.max-everyday.com/2018/06/python-3-urllib/
http://tw.gitbook.net/python/python3-webbug-series1.html
https://edu.tcfst.org.tw/edm/08C009/images/Python網路爬蟲%20.pdf